package com.blogspot.nhu313.data.utils;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import com.blogspot.nhu313.myhistory.category.CategorySQLStringBuilder;
import com.blogspot.nhu313.myhistory.service.db.DBField;
import com.blogspot.nhu313.myhistory.service.db.SQLStringBuilder;

public class SQLStringCreatorTest extends TestCase {
	List<DBField> DBColumns;

	public void setUp(){
		DBColumns = new ArrayList<DBField>();
	}
	
	public void tearDown(){
		DBColumns = null;
	}

	public void testCreateTable_withNullDBColumn(){
		try{
			SQLStringBuilder.createTable("name", null);
			fail();
		} catch (NullPointerException e){
			//expected
		}
	}

	public void testCreateTable_withNoDBColumn(){
		String expected = "CREATE TABLE IF NOT EXISTS name();";
		String actual = SQLStringBuilder.createTable("name", DBColumns);
		assertEquals(expected, actual);
	}
	
	public void testCreatTable_withDBColumns(){
		String expected = "CREATE TABLE IF NOT EXISTS Category(id INT PRIMARY KEY, name VARCHAR NOT NULL, parent_id INT);";
		
		List<DBField> columns = new ArrayList<DBField>();
		columns.add(new DBField("id", DBField.Type.INT_PRIMARY_KEY));
		columns.add(new DBField("name", DBField.Type.VARCHAR_NOT_NULL));
		columns.add(new DBField("parent_id", DBField.Type.INT));
		
		assertEquals(expected, SQLStringBuilder.createTable("Category", columns));
	}
	
	public void testQuery(){
		String expected = "SELECT name FROM tableName;";
		assertEquals(expected, SQLStringBuilder.query("tableName", "name"));
	}
	
	public void testQueryAllColumn(){
		String expected = "SELECT * FROM tableName;";
		assertEquals(expected, SQLStringBuilder.queryAllColumns("tableName"));
	}
	
	public void testQueryWithCondition(){
		String whereCondition = "name = \"J\"";
		String expected = "SELECT * FROM tableName WHERE " + whereCondition + ";";
		assertEquals(expected, SQLStringBuilder.queryWithCondition("tableName", "*", whereCondition ));
	}	

	
	public void testS(){
		String expected = "SELECT _id, name, parent_id FROM Category;";
		assertEquals(expected, CategorySQLStringBuilder.getCategories()); 
	}
}
